package com.sdp.irrigation.db.irrigation_eq_valve.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.irrigation.db.irrigation_eq_valve.mapper.IrrigationEqValveMapper;
import com.sdp.irrigation.db.irrigation_eq_valve.service.IIrrigationEqValveService;
import com.sdp.irrigation.entity.IrrigationEqValve;
import com.sdp.irrigation.utils.IrrigationConstants;

/**
 * <p>
 * 设备阀门信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-12-18
 */
@Service
public class IrrigationEqValveServiceImpl extends ServiceImpl<IrrigationEqValveMapper, IrrigationEqValve> implements IIrrigationEqValveService {

	

	@Override
	public Map<String,IrrigationEqValve> updateEqValve(String oldEqNo, String newEqNo) {
		//根据旧的设备编号，获取原来的阀门信息
		List<IrrigationEqValve> eqValveList = queryEqValveListByEqNo(oldEqNo);
		
		Map<String,IrrigationEqValve> eqValveMap = new LinkedHashMap<>();
		
		//将原来阀门信息的设备编号更新为新的设备编号
		if(eqValveList!=null && eqValveList.size()>0) {
			for(IrrigationEqValve valve : eqValveList) {
				valve.setEqNo(newEqNo);
				valve.setSwitchStatus(IrrigationConstants.阀门开关状态_关.getCode());
				valve.setUpdateTime(LocalDateTime.now());
				if(StringUtils.isEmpty(valve.getCreateTime())) {
					valve.setCreateTime(LocalDateTime.now());
				}
				//key为阀门序号
				eqValveMap.put(valve.getSerialNo().toString(), valve);
			}
			
			updateBatchById(eqValveList);
		}
		
		return eqValveMap;
	
	}

	@Override
	public List<IrrigationEqValve> queryEqValveListByEqNo(String eqNo) {
		LambdaQueryWrapper<IrrigationEqValve> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEqValve::getEqNo, eqNo);
		lambdaQueryWrapper.orderByAsc(IrrigationEqValve::getSerialNo);
		return list(lambdaQueryWrapper);
	}

	@Override
	public Map<String, IrrigationEqValve> queryEqValveMapByEqNo(String eqNo) {
		Map<String,IrrigationEqValve>eqValveMap = new HashMap<>();
		List<IrrigationEqValve> eqValveList = queryEqValveListByEqNo(eqNo);
		if(eqValveList!=null && eqValveList.size()>0) {
			for(IrrigationEqValve valve : eqValveList) {
				//key为阀门序号
				eqValveMap.put(valve.getSerialNo().toString(), valve);
			}
		}
		
		return eqValveMap;
	}

	@Override
	public void removeValveMapByEqNo(String eqNo) {
		LambdaQueryWrapper<IrrigationEqValve> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(IrrigationEqValve::getEqNo, eqNo);
		remove(lambdaQueryWrapper);
		
	}

}
